import sqlite3

class LazyDBReader:
    def __init__(self, db_path, batch_size=100):
        self.conn = sqlite3.connect(db_path)
        self.cursor = self.conn.cursor()
        self.batch_size = batch_size
        self.last_id = 0
        
    def get_next_batch(self):
        query = f"""
            SELECT id, name, value 
            FROM measurements 
            WHERE id > {self.last_id} 
            ORDER BY id 
            LIMIT {self.batch_size}
        """
        results = self.cursor.execute(query).fetchall()
        print("printing last_id", self.last_id)
        if not results:
            return None  # 哨兵值，表示没有更多数据
            
        self.last_id = results[-1][0]  # 更新最后一个ID
        return results
        
    def close(self):
        self.conn.close()

# 使用方式
db_reader = LazyDBReader('sensor_data.db', batch_size=5)
try:
    # 创建迭代器，当get_next_batch返回None时停止
    batches = iter(db_reader.get_next_batch, None)
    
    for batch in batches:
        for id, name, value in batch:
            # 处理每条记录...
            print(f"处理记录: {id}, {name}, {value}")
finally:
    db_reader.close()